package com.gcxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gcxy.Vo.DataDicValVO;
import com.gcxy.conf.result.R;
import com.gcxy.conf.result.ResultCodeEnum;
import com.gcxy.dto.ddvDTO;
import com.gcxy.entity.DataDicVal;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.DataDicValMapper;
import com.gcxy.mapper.RanchMapper;
import com.gcxy.service.DataDicValService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hy
 * @since 2023-08-16
 */
@Service
public class DataDicValServiceImpl extends ServiceImpl<DataDicValMapper, DataDicVal> implements DataDicValService {

    @Autowired
    private DataDicValMapper dataDicValMapper ;
    @Autowired
    private RanchMapper ranchMapper;

    @Override
    public R addDic(DataDicVal dataDicVal) {
        //1-判断前端传进来的对象是否为空
        if (ObjectUtils.isEmpty(dataDicVal)){
            return R.Failed(ResultCodeEnum.REQUEST_PARAM_NULL.getCode(),"请求参数为空");
        }
        //2-通过前端传来的数据字典类型编号 和 数据字典值 查询数据库中是存在已有信息 ,如果有 报错
        LambdaQueryWrapper<DataDicVal> lm= new LambdaQueryWrapper<>();
        lm.eq(DataDicVal::getValue,dataDicVal.getValue()).eq(DataDicVal::getDicId,dataDicVal.getDicId());
        if(!ObjectUtils.isEmpty(dataDicValMapper.selectOne(lm))){
            return R.Failed(ResultCodeEnum.DUPLICATE_KEY_EXCEPTION.getCode(),"该数字字典类型下,数据字典值重复");
        }
        //3-插入数据 , 并判断是否插入成功
        int result=dataDicValMapper.insert(dataDicVal);
        if(result<1){
            return R.Failed(ResultCodeEnum.REQUEST_OUT_POST_OVERTIME.getCode(),"插入失败");
        }
        return R.Success(null);
    }

    @Override
    public R queryDicValueList(ddvDTO ddv) {

        IPage<DataDicValVO> pages = new Page<>(ddv.getPage(),ddv.getPageSize());
        QueryWrapper<DataDicValVO> queryWrapper = new QueryWrapper<>();

            queryWrapper
                    .like( null!=ddv.getValue(),"data_dic_val.value",ddv.getValue())
                    .eq(null!=ddv.getDicId(),"data_dic_val.dic_id",ddv.getDicId())
                    .eq(null!=ddv.getDdvState(),"data_dic_val.ddv_state",ddv.getDdvState());

        List<DataDicValVO> list=dataDicValMapper.selectListByQuery(pages,queryWrapper);
        if(ObjectUtils.isEmpty(list)){
            return R.Failed(ResultCodeEnum.REQUEST_NOT_FOUND.getCode(),"查询为空");
        }

        Map<String,Object> map = new HashMap<>();
        map.put("pageNumber",pages.getCurrent());
        map.put("pageSize",pages.getSize());
        map.put("totalPages",pages.getPages());
        map.put("total",pages.getTotal());
        map.put("records",list);

        return R.Success(map);
    }

    @Override
    public R updateDdvState(Integer ddvId) {

        if (ObjectUtils.isEmpty(ddvId)){
            return R.Failed(ResultCodeEnum.REQUEST_PARAM_ERROR.getCode(),"数据值编号未传");
        }
        DataDicVal dataDicVal =dataDicValMapper.selectById(ddvId);
        if(ObjectUtils.isEmpty(dataDicVal)){
            return R.Failed(ResultCodeEnum.REQUEST_PARAM_NULL.getCode(),"该数据值编号对应数据值不存在");
        }
        //如果其他模块使用了该数据字典 , 不可禁用
        List<Ranch> listPas =ranchMapper.selectList(null);
        for (Ranch pas: listPas) {
            if(pas.getValId() == ddvId){
                return R.Failed(ResultCodeEnum.REQUEST_OUT_POST_OVERTIME.getCode(),"该数据字典值正在使用中");
            }
            break;
        }

        int newState = (dataDicVal.getDdvState()==1)?0:1;
        UpdateWrapper<DataDicVal> uw = new UpdateWrapper<>();
        uw.set("ddv_state",newState).eq("ddv_id",dataDicVal.getDdvId());
        int result  = dataDicValMapper.update(dataDicVal,uw);
        if(result<1){
            return R.Failed(ResultCodeEnum.REQUEST_OUT_POST_OVERTIME.getCode(),"修改失败");
        }
        return R.Success(null);

    }


}
